package com.yixiu.javabase.modules.io;

import com.sun.webkit.graphics.WCRectangle;

import java.io.*;


/**
 * io流总结
 */
public class IOStream {


    public static void main(String [] args) {

        //testByteArrayInputStream();
        //testFileInputStream();

        //copyFileByte(new File("C:\\test.txt"),new File("C:\\tt.txt"));

        copyFileChar(new File("C:\\test.txt"),new File("C:\\tt.txt"));
    }



    public  static void IOInterface() {

        // 字节流
        InputStream inputStream = null; // 顶部抽象类,
        OutputStream outputStream = null; // 顶部抽象类
        FileInputStream fileInputStream = null; // 文件输入流，继承自InputStream
        FileOutputStream fileOutputStream = null; // 文件输出流，继承自OutputStream


        StringBufferInputStream stringBufferInputStream = null;





        // 字符流
        Reader reader = null; // 顶部抽象类
        InputStreamReader inputStreamReader = null; // 字符输入流
        Writer writer = null; // 顶部抽象类
        OutputStreamWriter outputStreamWriter = null; // 字符输出流




    }

    public  static void testByteArrayInputStream(){
        String message = "Hello World !";
        byte []  messageBytes  = message.getBytes();// 获取字符串对应的字节数组
        // ByteArrayInputStream对象初始化必须有字节数组
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(messageBytes );
        int result = 0;
        while( (result = byteArrayInputStream.read()) != -1 ) {
            System.out.println( (char) result);
        }
        try {
            byteArrayInputStream.close();
        } catch (IOException ee) {
            ee.printStackTrace();
        }
    }

    public  static void testFileInputStream() {
        try {
            FileInputStream fileInputStream = new FileInputStream("C:\\test.txt");
            int result = 0;
            while ( (result = fileInputStream.read() ) != -1 ) {
                System.out.println((char)result);
            }
            fileInputStream.close();
        } catch (Exception ee) {
            ee.printStackTrace();
        }
    }

    public static  void testBufferedInputStream() throws  Exception {
        FileInputStream fileInputStream = new FileInputStream("C:\\test.txt");
        // 初始化必须是InputStream
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);



    }


    /**
     * 复制文件
     * @param oldFile
     * @param newFile
     */
    public static void copyFileByte( File oldFile , File newFile) {
        InputStream inputStream = null;
        BufferedInputStream bufferedInputStream = null;

        OutputStream outputStream = null;
        BufferedOutputStream bufferedOutputStream = null;

        try {
            inputStream = new FileInputStream(oldFile);
            bufferedInputStream = new BufferedInputStream(inputStream);

            outputStream = new FileOutputStream(newFile);
            bufferedOutputStream = new BufferedOutputStream(outputStream);

            byte[] bytes = new byte[1024];   //代表一次最多读取1KB的内容

            int length = 0; //代表实际读取的字节数
            while ((length = bufferedInputStream.read(bytes)) != -1) {
                //length 代表实际读取的字节数
                bufferedOutputStream.write(bytes, 0, length);
            }
            //缓冲区的内容写入到文件
            bufferedOutputStream.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            if (bufferedOutputStream != null) {
                try {
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedInputStream != null) {
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }



    private static void copyFileChar( File oldFile , File newFile ){
        Reader reader = null ;
        BufferedReader bufferedReader = null ;

        Writer writer = null ;
        BufferedWriter bufferedWriter  = null ;
        try {
            reader = new FileReader( oldFile ) ;
            bufferedReader = new BufferedReader( reader ) ;

            writer = new FileWriter( newFile ) ;
            bufferedWriter = new BufferedWriter( writer ) ;

            String result = null ; //每次读取一行的内容
            while (  (result = bufferedReader.readLine() ) != null ){
                bufferedWriter.write( result );  //把内容写入文件
                bufferedWriter.newLine();  //换行，result 是一行数据，所以没写一行就要换行
            }
            bufferedWriter.flush();  //强制把数组内容写入文件
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bufferedWriter.close();  //关闭输出流
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bufferedReader.close();  //关闭输入流
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
